<!--
 * @Description: 邀请有礼商品列表
 * @Author: liyujie
 * @Date: 2021/2/23
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/2/23
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <el-tabs v-model="active">
                <el-tab-pane name="1">
                    <span slot="label">
                        展示商品
                        <el-popover
                            placement="bottom-start"
                            width="200"
                            trigger="hover"
                            content="邀请有礼页展示的商品"
                        >
                            <i
                                class="el-icon-question"
                                slot="reference"
                            />
                        </el-popover>
                    </span>
                </el-tab-pane>
                <el-tab-pane name="2">
                    <span slot="label">
                        黑名单商品
                        <el-popover
                            placement="bottom-start"
                            width="200"
                            trigger="hover"
                            content="不参与用户实付满1元活动的商品"
                        >
                            <i
                                class="el-icon-question"
                                slot="reference"
                            />
                        </el-popover>
                    </span>
                </el-tab-pane>
            </el-tabs>
            <!-- /header -->
            <template v-if="active === '1'">
                <!-- 筛选-->
                <div class="filter-view">
                    <div class="header-new-box">
                        <p class="header-new-box-title">
                            商品名称
                        </p>
                        <el-input
                            placeholder="请输入商品名称"
                            v-model="filterData.keyword"
                            suffix-icon="el-icon-search"
                            clearable
                            size="medium"
                            style="width: 286px"
                            @keydown.enter.native="getTableData(1)"
                        />
                    </div>
                    <div class="header-new-box">
                        <p class="header-new-box-title">
                            商品状态
                        </p>
                        <el-select
                            v-model="filterData.status"
                            clearable
                            filterable
                            placeholder="全部"
                            size="medium"
                            @change="getTableData(1)"
                        >
                            <el-option
                                label="全部"
                                value=""
                            />
                            <el-option
                                label="已上架"
                                :value="1"
                            />
                            <el-option
                                label="已下架"
                                :value="0"
                            />
                        </el-select>
                    </div>

                    <div class="header-new-box">
                        <el-button
                            type="primary"
                            size="small"
                            @click="getTableData(1)"
                        >
                            查询
                        </el-button>
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-plus"
                            @click="$refs.addGoodsDialog.show('add')"
                        >
                            添加商品
                        </el-button>
                    </div>
                </div>
                <!--筛选-->

                <!-- table -->
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    ref="tempLateName"
                    class="data-table"
                    v-loading="tableLoading"
                >
                    <el-table-column
                        label="序号"
                        align="center"
                        width="80"
                        type="index"
                    />
                    <el-table-column
                        prop="title"
                        label="商品标题"
                        show-overflow-tooltip
                        align="left"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="商品缩略图"
                        show-overflow-tooltip
                        align="center"
                    >
                        <template slot-scope="scope">
                            <img
                                :src="scope.row.cover"
                                style="width: 60px;height: 60px;cursor: pointer"
                                @click="fnShowPreview(scope.row.cover)"
                            >
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="goods_no"
                        label="商品编号"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="price"
                        label="显示价格"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="sort"
                        label="排序"
                        show-overflow-tooltip
                        align="center"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        label="状态"
                        align="center"
                        :formatter="emptyFormatter"
                    >
                        <template slot-scope="scope">
                            <div v-if="scope.row.status === 1">已上架</div>
                            <div v-if="scope.row.status === 0">已下架</div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        align="left"
                        width="160"
                    >
                        <template slot-scope="scope">
                            <div class="option-btn-group">
                                <el-button
                                    type="text"
                                    size="medium"
                                    v-if="scope.row.status === 1"
                                    @click="$refs.addGoodsDialog.show('edit', scope.row)"
                                >
                                    编辑
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    @click="handleModify(scope.row)"
                                >
                                    {{ scope.row.status === 1 ? '下架' : '上架' }}
                                </el-button>
                                <el-button
                                    type="text"
                                    size="medium"
                                    v-if="scope.row.status === 0"
                                    @click="deleteGoods(scope.row.goods_no)"
                                >
                                    删除
                                </el-button>
                            </div>
                        </template>
                    </el-table-column>
                </el-table>
                <!-- /table -->

                <!-- /分页 -->
                <div class="pagination-wrapper clearfix">
                    <el-pagination
                        layout="total, prev, pager, next, jumper"
                        background
                        :current-page.sync="page.now_page"
                        :total="page.total_count"
                        :page-size="page.page_size"
                        @current-change="getTableData"
                    />
                </div>
            </template>
            <polite-blacklist
                v-if="active === '2'"
            />
        </div>

        <!-- 图片预览 -->
        <img-preview ref="preview"/>
        <!-- 添加商品弹窗 -->
        <add-goods-dialog
            ref="addGoodsDialog"
            @update="getTableData()"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import ImgPreview from "@/components/common/img-preview";
import AddGoodsDialog from "../../../components/polite/add-goods-dialog";
import PoliteBlacklist from "./polite-blacklist";

export default {
    name: 'PoliteGoodsList',
    mixins: [back],
    components: {PoliteBlacklist, AddGoodsDialog, ImgPreview},
    data() {
        return {
            // tab 1展示商品 2黑名单商品
            active: '1',
            filterData: {
                keyword: '',
                status: '',
            },
            tableData: [],
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            authResult: {}
        };
    },
    computed: {},
    watch: {},

    mounted() {
        this.$store.commit('SET_HEADER', [{title: "APP后台"}, {title: '商城'}, {title: '邀请有礼'},{title: '商品详情'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {

            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/student_user_invite_award_goods%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 预览图片
         * @param   {String}    images      //单图预览
         * @param   {Array}     images      //多图预览
         * @param index
         */
        fnShowPreview(images, index) {
            if (images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images, index);
            } else {
                this.$message({
                    type: 'warning',
                    message: '图片类型错误'
                });
            }
        },

        /**
         * 商品上下架
         * @param row
         */
        handleModify(row) {
            if(!this.authResult.auth_modify) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm(`确认${row.status === 1 ? '下架' : '上架'}该商品吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: row.goods_no,
                    status: row.status === 1 ? 0 : 1
                };

                this.$post('/student/student_user_invite_award_goods%5Cset_status', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '成功',
                            message: '操作成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        },

        /**
         * 删除商品
         * @param no
         */
        deleteGoods(no) {
            if(!this.authResult.auth_delete) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }

            this.$confirm('确认删除该商品吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    goods_no: no
                };

                this.$post('/student/student_user_invite_award_goods%5Cdelete', params, resp => {
                    if (resp.code === 1) {
                        this.$notify({
                            title: '提示',
                            message: '删除成功',
                            duration: 2000,
                            type: 'success'
                        });
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        }
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;
    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        background: #fff;
        box-sizing: border-box;
        /*el-tabs*/

        /deep/ .el-tabs {
            .el-tabs__nav-wrap {
                height: 54px;
                padding-left: 24px;

                .el-tabs__nav-scroll {
                    .el-tabs__nav {
                        height: 54px;
                        line-height: 54px;
                    }
                }
            }
        }

        /*header*/

        .header {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin-left: 12px;
                margin-bottom: 24px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
            .add-btn {
                margin-top: -16px;
            }
        }

        /*筛选*/

        .filter-view {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;
            padding: 0 24px;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 24px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        // 表格
        .data-table {
            padding: 0 24px;
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding: 10px 24px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>
